/*
 * State_Machine.h
 *
 *  Created on: Oct 2, 2024
 *      Author: Grasping
 */
#ifndef MY_INC_APP_APP_ACLOAD_LOADPOWERSTATEMACHINE_H_
#define MY_INC_APP_APP_ACLOAD_LOADPOWERSTATEMACHINE_H_
#include "MY_INC/APP/APP_ACLoad_Protection.h"
#include "MY_INC/APP/APP_ACLoad_Pwm.h"
#include "MY_INC/APP/APP_Init.h"
#include "MY_INC/LC/LC_SYS.h"
#include "stdbool.h"

static inline void RunLoadPowerLoop_State_ProcessControl(InterruptController *controller);
static inline void RunLoadPowerLoop_State_Idle(InterruptController *controller); // 空闲状态处理
// static inline void RunLoadPowerLoop_State_WaitLock(InterruptController *controller);      // 等待锁相状态处理
static inline void RunLoadPowerLoop_State_WaitCrossZero(InterruptController *controller); // 等待过零点状态处理
static inline void RunLoadPowerLoop_State_Running(InterruptController *controller);       // 运行状态处理
static inline void RunLoadPowerLoop_State_Machine(InterruptController *controller);       // 状态机主控函数
static inline void RunLoadPowerLoop_State_ProcessControl(InterruptController *Controller)
{
    RunLoadPowerLoop_State_Machine(Controller); // 运行无PLL状态机
    SPWM_Control_Inline(&Controller->pwm);      // 传递整个 controller 和 PWM控制
}

static inline void RunLoadPowerLoop_State_Machine(InterruptController *controller)
{

    switch (controller->loop_sm.current_state) // 访问 loop_sm.current_state
    {
    case STATE_IDLE:
        RunLoadPowerLoop_State_Idle(controller); // 传递整个 controller
        break;
    case STATE_WAIT_CROSS_ZERO:                           // 枚举名称从 STATE_WAITCROSSZERO 改为 STATE_WAIT_CROSS_ZERO
        RunLoadPowerLoop_State_WaitCrossZero(controller); // 传递整个 controller
        break;
    case STATE_RUN:                                 // 枚举名称从 STATE_RUN 改为 STATE_RUN
        RunLoadPowerLoop_State_Running(controller); // 传递整个 controller
        break;
    default:
        break;
    }
}
static inline void RunLoadPowerLoop_State_Idle(InterruptController *controller)
{
    switch (controller->loop_sm.event) // 访问 loop_sm.event
    {

    case EVENT_LOOP_NONE:                               // 无事件
        controller->loop_sm.current_state = STATE_IDLE; // 直接更新 current_state
        break;
    default: // 其他事件不响应
        controller->loop_sm.current_state = STATE_IDLE;
        break;
    }
}
// 等待过零点状态处理 (参数类型修正，内部访问 controller->param 和 controller->loop_sm)
static inline void RunLoadPowerLoop_State_WaitCrossZero(InterruptController *controller)
{
    switch (controller->loop_sm.event) // 访问 loop_sm.event
    {

    case EVENT_LOOP_NONE: // 无事件

        if (controller->param.pll->Theta < 2.0f / 360.0f * _2PI) // 等待过零点
        {
            LL_HRTIM_TIM_SetCompare1(HRTIM1, controller->pwm.timer[0], 0);
            LL_HRTIM_TIM_SetCompare1(HRTIM1, controller->pwm.timer[1], 0);
            controller->loop_sm.current_state = STATE_RUN; // 直接更新 current_state
            break;
        }
        controller->loop_sm.current_state = STATE_WAIT_CROSS_ZERO; // 默认切换到等待锁相状态
        break;
    default:                                            // 其他事件不响应
        controller->loop_sm.current_state = STATE_IDLE; // 直接更新 current_state
        break;
    }
}

/*---------- 运行状态处理 ----------*/
// 运行状态处理 (参数类型修正，内部访问 controller->param 和 controller->loop_sm)
static inline void RunLoadPowerLoop_State_Running(InterruptController *controller)
{
    switch (controller->loop_sm.event) // 访问 loop_sm.event
    {
    case EVENT_LOOP_NONE: // 无事件
        VariableStep_Update_Inline(controller->param.peref, controller->param.pe_ref);
//        VariableStep_Update_Inline(controller->param.qeref, controller->param.qe_ref);
        /*此时处于PFC模式，电流方向取负*/

        float Idref = AntiWindup_PID_Control_Inline(controller->param.pi_pe, controller->param.peref->currentTemp,
                                                    controller->param.pq->Pe_Actual);
//        float Iqref = AntiWindup_PID_Control_Inline(controller->param.pi_qe, controller->param.qeref->currentTemp,
//                                                    controller->param.pq->Qe_Actual);

        float iref = Idref * controller->param.pll->Sine; //+ Iqref * controller->param.pll->Cosine;
        controller->pwm.mod_wave = (PMRdiff_Calculate_Inline(controller->param.pr_i, iref,
                                                             controller->param.il_actual) + // 主要处理三次谐波
                                    controller->param.vo_actual) /
                                   controller->param.trap_udc->notch.Yn;

        controller->pwm.pwm_en_flag = Activated; // 允许PWM
        /*默认转移*/
        controller->loop_sm.current_state = STATE_RUN; // 直接更新 current_state
        break;
    default:                                            // 其他事件不响应
        controller->loop_sm.current_state = STATE_IDLE; // 直接更新 current_state
        break;
    }
}

#endif /* INC_STATE_MACHINE_H_ */
